public with sharing class SyncNCController {


	public String backId {get;private set;}
	
	public String  msg {get;private set;} 

	public String type {get;private set;}

	public String dataJson {get;private set;}

    public SyncNCController() {
    


    }

    public void setNil(){

    	dataJson = '[]';
    }
    public void getDetails(){



    }


    public void toPriceList(){

    	ResultBean res = null;
    	try {

    	    AdjustPrice__c ap =[select Id,Account__r.AccountNumber,WhetherSynchronizeNC__c ,WhetherReverse__c from AdjustPrice__c where Id=:backId];

    		if(ap.WhetherReverse__c == true )throw new DmlException('同步并反写到价目表成功');

    		String msg = AdjustPriceHandler.adjustPriceToPriceList(ap.Id);

    		msg = msg == 'OK' ? '同步并反写到价目表成功!': '同步成功!但反写到价目表失败:'+msg;
    		res = new ResultBean(msg ,null ,0);

    	}catch (Exception e) {
    	    
    		res = new ResultBean(e.getMessage() ,null ,0);

    	}
    	dataJson = EncodingUtil.urlEncode(JSON.serialize(res), 'UTF-8');

    	
    }

    public void getAdpriceDetails(){
    	ResultBean res = null;
    	try{

    		AdjustPrice__c ap =[select Id,Account__r.AccountNumber,WhetherSynchronizeNC__c ,WhetherReverse__c from AdjustPrice__c where Id=:backId];

	        if(ap.WhetherReverse__c == true && ap.WhetherSynchronizeNC__c == true) throw new DmlException('已同步, 请勿重复点击同步!');

	        List<AdjustPriceProduct__c> apList = [select Id,Product__r.Name  ,Product__r.NCCode__c,AdjustPrice__r.Account__r.AccountNumber,
	        AdjustPrice__r.SalesCode__c,OriginalPrice__c,AdjustSalesPrice__c,
	        AdjustPrice__r.OwnerId,AdjustPrice__r.Owner.Name ,AdjustPrice__r.CreatedBy.Name ,AdjustPrice__r.Company__c
	          from AdjustPriceProduct__c where AdjustPrice__c =:backId ];

	      	if(apList.size() == 0)throw new DmlException('没有需要同步的数据!');

	       	AdjustPriceProduct__c ap1  = apList[0];
	        User u = [select Id, Manager.Name from User where Id =:ap1.AdjustPrice__r.OwnerId];
	        List<ConProductArgs> conProductArgsList = new List<ConProductArgs>();
	        Map<String,String>comMap =  ImportObject.getPicklistValues('Contract__c', 'Corpcode__c', false);
	        Map<String,String>sMap  =  ImportObject.getPicklistValues('AdjustPrice__c', 'SalesCode__c', true);
	        for (AdjustPriceProduct__c app: apList) {
	            ConProductArgs cpa = new ConProductArgs();
	            cpa.corpcode = comMap.get( sMap.get(app.AdjustPrice__r.SalesCode__c));  //公司编码
	            cpa.custcode = app.AdjustPrice__r.Account__r.AccountNumber;  //客户编码
	            cpa.vsalestrucode = app.AdjustPrice__r.SalesCode__c ; //销售组织编码
	            cpa.created_by = app.AdjustPrice__r.Owner.Name; //创建人名字
	            cpa.sub_corp_manager = u.Manager.Name;  //报价的销售中心总监名字
	            //cpa.csourcecode = cp.Contract__r.ContractNumber__c; //合同自动编号
	            cpa.ts = Utils.date2string(System.now()) ; //合同的lastmodifieddate
	            cpa.cemployeecode = app.AdjustPrice__r.Owner.Name; //所有者
	            cpa.invcode = app.Product__r.NCCode__c; //产品的NCCode__c
	            cpa.dreceiptdate  = Utils.date2string(Date.valueOf(System.now()));//报价日期

	            if(app.AdjustSalesPrice__c == null)throw new DmlException(app.Product__r.Name  + '没有价格,不能同步!');

	            cpa.noriginalcurtaxprice = String.valueof(app.AdjustSalesPrice__c); 
	            
	           //SalesPrice__c
	            cpa.nitemdiscountrate = '100';//默认100
	            //cpa.nnumber = String.valueof(cp.Number__c);
	            cpa.opertype = '1'; //操作类型   1=新增  2=修改  3=删除
	            cpa.quotetype = '1'; //报价类型   1=报价  2=调价
	            cpa.csalequotationid = backId;
	            cpa.csalequotation_bid = app.Id;
	            cpa.currtypename = '人民币';
	           // cpa.addCustomer =cp.Contract__r.TerminalCustomer__r.NCICCode__c;
	            conProductArgsList.add(cpa);
	           
	        }

    		res = new ResultBean('success' ,conProductArgsList ,0);

    	}catch(Exception e ){

    		res = new ResultBean(e.getMessage() ,null ,1);

    	}

    	dataJson = EncodingUtil.urlEncode(JSON.serialize(res), 'UTF-8');

    	
    }


    public class ConProductArgs {

        public String corpcode;  //报价上新增字段 公司编码 报价.corpcode__c
        public String custcode;  //客户的NCAccountNumber__c
        public String vsalestrucode;  //报价上新增字段 销售组织编码 报价.vsalestrucode__c
        public String created_by;  //报价的创建人名字
        public String sub_corp_manager;  //报价的销售中心总监名字
        public String csourcecode;  //报价的QuoteNumber
        public String ts;  //报价的lastmodifieddate
        public String csalequotationid;  //报价的id
        public String csalequotation_bid;  //报价产品的id
        public String cemployeecode;  //报价所有者
        public String invcode;  //报价产品.产品的NCCode__c
        public String noriginalcurtaxprice;  //报价产品的SalesPrice__c
        public String nitemdiscountrate;  //报价产品的DiscountRate__c
        public String nnumber;  //报价产品的Quantity__c
        public String opertype; //操作类型   1=新增  2=修改  3=删除
        public String quotetype; //报价类型   1=报价  2=调价
        public String adjustid; //调价的id
        public String adjust_pid; //调价产品的id
        public String addCustomer; //客户终端类型
        public String currtypename ; //币种
        public String dreceiptdate;//报价日期

    }

    public class ResultBean{
    	public String msg;
    	public Object data;
    	public Integer code;

    	public ResultBean(String msg ,Object data,Integer code ){
    		this.msg = msg;
    		this.data = data;
    		this.code = code;

    	}
    }


}